package com.school.controller;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.entity.HljtClass;
import com.school.entity.HljtTeacher;
import com.school.entity.HljtTeacherclass;
import com.school.entity.web.ClassTeacher;
import com.school.entity.web.TeacherClass;
import com.school.service.HljtOtherInterfacesService;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtTeacherCurriculumServiceImpl;
import com.school.service.impl.HljtTeacherServiceImpl;
import com.school.service.impl.HljtTeacherclassServiceImpl;
import com.school.util.Message;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="HljtTeacherAndClassInfoController")
public class HljtTeacherCurriculumController {

	@Autowired 
	HljtTeacherCurriculumServiceImpl oHljtTeacherCurriculumService;
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	@Autowired
	private HljtTeacherServiceImpl hljtTeacherServiceImpl;
	@Autowired
	private HljtTeacherclassServiceImpl hljtTeacherclassServiceImpl;
	@Autowired
	HljtOtherInterfacesService hljtOtherInterfacesService;
	/**
	 *  
	 * @param strType 班主任 或者 所有老师
	 * @return 老师列表，班级列表
	 * @throws Exception 
	 */
	@RequestMapping(value="/findAllTeacherAndClasses")
	@ResponseBody
	public Message selectClasssCheduleCard(String teachername,Integer pageSize,Integer  pageIndex,HttpServletResponse rep) throws Exception{
		RepUtil.repPake(rep);  
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		if(null!=teachername&&teachername.length()>0) {
			pageIndex=(pageIndex-1)*pageSize;
			List<HljtTeacher> HljtTeacherclass=hljtTeacherclassServiceImpl.likefindteacher(teachername, pageSize, pageIndex);
			List<HljtTeacher> HljtTeacherclassadd=hljtTeacherclassServiceImpl.likefindteacher(teachername, null, null);
			List<ClassTeacher> ClassTeacher=new ArrayList<>();
			for (int i = 0; i < HljtTeacherclass.size(); i++) {
				ClassTeacher classTeacher=new ClassTeacher();
				int teacherid=HljtTeacherclass.get(i).getId();
				HljtTeacher teacher =hljtTeacherServiceImpl.findTeacher(teacherid);
				classTeacher.setId(HljtTeacherclass.get(i).getId());
				classTeacher.setTeacherid(teacherid);
				classTeacher.setName(teacher.getName());
				classTeacher.setSubject(teacher.getSubject());
				classTeacher.setImage(newIp+teacher.getImages());
				classTeacher.setIntroduction(teacher.getIntroduction());
				
				List<TeacherClass> Teacher=hljtTeacherclassServiceImpl.selectteacher(teacherid);
				
				List<TeacherClass> TeacherClass=new ArrayList<>();
				for (int j = 0; j < Teacher.size(); j++) {
					TeacherClass teacherClass=new TeacherClass();
					teacherClass.setClasses(Teacher.get(j).getClasses());
					teacherClass.setGrade(Teacher.get(j).getGrade());
					teacherClass.setType(Teacher.get(j).getType());
					teacherClass.setClassid(Teacher.get(j).getClassid());
					TeacherClass.add(teacherClass);
				}
				classTeacher.setTeacherclasses(TeacherClass);
				ClassTeacher.add(classTeacher);
			}
			data.put("data", ClassTeacher);
			List<HljtTeacher> hljtTeacher=hljtTeacherServiceImpl.findTeacherall();
			List<HljtClass> HljtClass=hljtClassServiceIpml.findClasslist();
			data.put("number", HljtTeacherclassadd.size());
			data.put("classes", HljtClass);
			data.put("teacher", hljtTeacher);
			data.put("success", true);
		}else {
			List<HljtTeacherclass> HljtTeacherclassall=hljtTeacherclassServiceImpl.findteacherall(null, null);
			
			pageIndex=(pageIndex-1)*pageSize;
			List<HljtTeacherclass> HljtTeacherclass=hljtTeacherclassServiceImpl.findteacherall(pageSize, pageIndex);
			List<ClassTeacher> ClassTeacher=new ArrayList<>();
			for (int i = 0; i < HljtTeacherclass.size(); i++) {
				//获取老师id
				int teacherid=HljtTeacherclass.get(i).getTeacherid();
				HljtTeacher Teacher=hljtTeacherServiceImpl.findTeacher(teacherid);
				ClassTeacher classTeacher=new ClassTeacher();
				
				classTeacher.setSubject(Teacher.getSubject());
				classTeacher.setIntroduction(Teacher.getIntroduction());
				classTeacher.setImage(newIp+Teacher.getImages());
				classTeacher.setName(Teacher.getName());
				classTeacher.setTeacherid(teacherid);
				
				List<HljtTeacherclass> Teacherclass=hljtTeacherclassServiceImpl.getteacher(teacherid);
				List<TeacherClass> teacherclass=new ArrayList<>();
				for (int j = 0; j < Teacherclass.size(); j++) {
					Integer classid =Teacherclass.get(j).getClassid();
					
					HljtClass Classes=hljtClassServiceIpml.findClass(classid);
					
					TeacherClass TeacherClass=new TeacherClass();
					TeacherClass.setClasses(Classes.getClasses());
					TeacherClass.setClassid(classid);
					TeacherClass.setGrade(Classes.getGrade());
					TeacherClass.setType(Teacherclass.get(j).getType());
					teacherclass.add(TeacherClass);
				}
				classTeacher.setTeacherclasses(teacherclass);
				ClassTeacher.add(classTeacher);
				
			}
			data.put("data", ClassTeacher);
			List<HljtTeacher> hljtTeacher=hljtTeacherServiceImpl.findTeacherall();
			List<HljtClass> HljtClass=hljtClassServiceIpml.findClasslist();
			data.put("number", HljtTeacherclassall.size());
			data.put("classes", HljtClass);
			data.put("teacher", hljtTeacher);
			data.put("success", true);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 添加 老师课程安排
	 * @param nType
	 * @param classIds
	 * @param teacherId
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/addCurriculumInfo")
	@ResponseBody
	public Message insertCurriculumInfo(String addTeacherData,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null!=addTeacherData) {
			
			JSONObject json = JSONObject.fromObject(addTeacherData);  
			Integer teacherid=Integer.parseInt(json.getString("teacherId"));
			String subject=json.getString("subject");
			List<Integer> list=json.getJSONArray("classIds");
			
			hljtTeacherclassServiceImpl.deleteclassteacher(teacherid);
			for (int i = 0; i < list.size(); i++) {
				HljtTeacherclass hljtTeacherclass=new HljtTeacherclass();
				Integer classid=list.get(i);
				hljtTeacherclass.setClassid(classid);
				hljtTeacherclass.setSubject(subject);
				hljtTeacherclass.setTeacherid(teacherid);
				hljtTeacherclass.setType(1);
				
				Integer number =hljtTeacherclassServiceImpl.selsectClaissTeachers(classid, teacherid);
				Integer numbers =hljtTeacherclassServiceImpl.selsectClaissTeacher(classid, teacherid);
				if(null!=number&&number==1) {
				}else if(null!=numbers&&numbers==1){
				}else {
					
					hljtTeacherclassServiceImpl.addclassteacher(hljtTeacherclass);
				}
			}
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
		}
	
	/**
	 * 删除老师课程安排
	 * @param nType
	 * @param classIds
	 * @param teacherId
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/deleteCurriculumInfo")
	@ResponseBody
	public Message deleteCurriculumInfo(String teachername,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=teachername&&teachername.length()>0) {
			HljtTeacher  hljtTeacher=hljtTeacherServiceImpl.getTeacher(teachername);
			hljtTeacherclassServiceImpl.deleteclassteacher(hljtTeacher.getId());
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	 
	/**
	 * 查找班主任
	 * @param rep
	 * @return
	 * @throws UnknownHostException 
	 */
 	@RequestMapping(value="/insertCurriculumInfo")
	@ResponseBody
	public Message selectTeacher(Integer classid,Integer pageSize,Integer pageIndex,String teachername,HttpServletResponse rep) throws UnknownHostException{
		RepUtil.repPake(rep);
		Message Message=new Message();//
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		if(null!=classid) {
				classid=null;
				if(null!=teachername&&teachername.length()>0) {
					List<HljtTeacher> HljtTeacher=hljtTeacherServiceImpl.findLikeTeacher(teachername);
					List<ClassTeacher> ClassTeacher=new  ArrayList<>();
					for (int i = 0; i < HljtTeacher.size(); i++) {
						ClassTeacher classTeacher=new ClassTeacher();
						Integer Teacherid =HljtTeacher.get(i).getId();
						HljtTeacherclass Teacher =hljtTeacherclassServiceImpl.findTeachers(Teacherid);
						classTeacher.setImage(newIp+HljtTeacher.get(i).getImages());
						classTeacher.setIntroduction(HljtTeacher.get(i).getIntroduction());
						classTeacher.setName(HljtTeacher.get(i).getName());
						classTeacher.setSubject(HljtTeacher.get(i).getSubject());
						classTeacher.setId(Teacher.getId());
						classTeacher.setTeacherid(Teacherid);
						Integer classId =Teacher.getClassid();
						classTeacher.setSubject(Teacher.getSubject());
						
						List<TeacherClass> teacherclasses =new ArrayList<>();
						HljtClass HljtClass=hljtClassServiceIpml.findClass(classId);
						TeacherClass TeacherClass=new TeacherClass();
						TeacherClass.setClassid(classId);
						TeacherClass.setClasses(HljtClass.getClasses());
						TeacherClass.setGrade(HljtClass.getGrade());
						teacherclasses.add(TeacherClass);
						
						classTeacher.setTeacherclasses(teacherclasses);
						ClassTeacher.add(classTeacher);
					}
					List<HljtClass> HljtClass=hljtClassServiceIpml.findClasslist();
					List<HljtTeacher> HljtTeachers=hljtTeacherServiceImpl.findTeacherall();
					data.put("class", HljtClass);
					data.put("teacher", HljtTeachers);
					data.put("data", ClassTeacher);
					data.put("success", true);
				}else {
					pageIndex=(pageIndex-1)*pageSize;
					List<HljtTeacherclass> Teacherclass=hljtTeacherclassServiceImpl.getTeacherall(classid,pageSize,pageIndex);
					List<HljtTeacherclass> Teacherclassall=hljtTeacherclassServiceImpl.getTeacherall(classid,null,null);
					
					List<ClassTeacher> ClassTeacher=new  ArrayList<>();
					for (int i = 0; i < Teacherclass.size(); i++) {
						ClassTeacher classTeacher=new ClassTeacher();
						Integer Teacherid=Teacherclass.get(i).getTeacherid();
						HljtTeacher	Teacher=hljtTeacherServiceImpl.findTeacher(Teacherid);
						classTeacher.setId(Teacherclass.get(i).getId());
						classTeacher.setName(Teacher.getName());
						classTeacher.setImage(newIp+Teacher.getImages());
						classTeacher.setTeacherid(Teacherid);
						classTeacher.setIntroduction(Teacher.getIntroduction());
						classTeacher.setSubject(Teacher.getSubject());
						
						List<TeacherClass> teacherclasses =new ArrayList<>();
						Integer teacherclassid =Teacherclass.get(i).getClassid();
						HljtClass HljtClass=hljtClassServiceIpml.findClass(teacherclassid);
						TeacherClass TeacherClass=new TeacherClass();
						TeacherClass.setClassid(teacherclassid);
						TeacherClass.setClasses(HljtClass.getClasses());
						TeacherClass.setGrade(HljtClass.getGrade());
						teacherclasses.add(TeacherClass);
						
						classTeacher.setTeacherclasses(teacherclasses);
						ClassTeacher.add(classTeacher);
					}
					List<HljtClass> HljtClass=hljtClassServiceIpml.findClasslist();
					List<HljtTeacher> HljtTeacher=hljtTeacherServiceImpl.findTeacherall();
					data.put("class", HljtClass);
					data.put("teacher", HljtTeacher);
					data.put("number", Teacherclassall.size());
					data.put("data", ClassTeacher);
					data.put("success", true);
				}
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
 }
 	
 	/**
 	 * 添加班级班主任信息
 	 * @param classid
 	 * @param teacherid
 	 * @param subject
 	 * @param rep
 	 * @return
 	 */
 	@RequestMapping(value="/addCurriculumInfor")
	@ResponseBody
	public Message addTeacher(Integer classid,Integer teacherid,String subject,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();//
		Map<String, Object> data =new HashMap<>();
		if(null!=classid&&null!=teacherid&&null!=subject) {
			
			Integer number =hljtTeacherclassServiceImpl.selsectClaissTeacher(classid,null);
			
			Integer numbers =hljtTeacherclassServiceImpl.selsectClaissTeacher(null,teacherid);
			HljtTeacherclass hljtTeacherclass=new HljtTeacherclass();
			hljtTeacherclass.setClassid(classid);
			hljtTeacherclass.setSubject(subject);
			hljtTeacherclass.setTeacherid(teacherid);
			hljtTeacherclass.setType(0);
			if(null!=number&&number==1) {
				data.put("success", false);
				data.put("info", "本班已存在班主任");
			}else if(null!=numbers&&numbers==1){
				data.put("success", false);
				data.put("info", "本老师已经是别的班班主任");
			}else {
				
				hljtTeacherclassServiceImpl.addclassteacher(hljtTeacherclass);
				hljtOtherInterfacesService.modifyByCalssId(classid);
				data.put("success", true);
			}
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
 	}
 	/**
 	 * 修改班级班主任信息
 	 * @param id
 	 * @param classid
 	 * @param teacherid
 	 * @param subject
 	 * @param rep
 	 * @return
 	 */
 	@RequestMapping(value="/updateCurriculumInfor")
	@ResponseBody
	public Message updateTeacher(Integer id,Integer classid,Integer teacherid,String subject,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();//
		Map<String, Object> data =new HashMap<>();
		if(null!=id) {
			Integer number =hljtTeacherclassServiceImpl.selsectClaissTeacher(classid,null);
			Integer numbers =hljtTeacherclassServiceImpl.selsectClaissTeacher(null,teacherid);
			if(null!=numbers&&numbers==1) {
				data.put("success", false);
				data.put("info", "本老师已经是别的班班主任");
			}else if(null!=number&&number==1){
				data.put("success", false);
				data.put("info", "本班已存在班主任");
			}else {
				HljtTeacherclass hljtTeacherclass=new HljtTeacherclass();
				hljtTeacherclass.setClassid(classid);
				hljtTeacherclass.setSubject(subject);
				hljtTeacherclass.setTeacherid(teacherid);
				hljtTeacherclass.setId(id);
				hljtTeacherclassServiceImpl.updateTeacher(hljtTeacherclass);
				hljtOtherInterfacesService.modifyByCalssId(classid);
				data.put("success", true);
			}
			
		}else {
			data.put("success", false);
		}
		List<HljtClass> HljtClass=hljtClassServiceIpml.findClasslist();
		List<HljtTeacher> HljtTeacher=hljtTeacherServiceImpl.findTeacherall();
		data.put("class", HljtClass);
		data.put("teacher", HljtTeacher);
		Message.setData(data);
		return Message;
 	}
 	
 	/**
 	 * 删除班主任
 	 * @param classid
 	 * @param teacherid
 	 * @param rep
 	 * @return
 	 */
 	@RequestMapping(value="/deleteCurriculumInfors")
	@ResponseBody
	public Message deleteTeacher(Integer classid , Integer id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();//
		Map<String, Object> data =new HashMap<>();
		if(null!=id) {
			hljtTeacherclassServiceImpl.deleteTeachers(id);
			hljtOtherInterfacesService.modifyByCalssId(classid);
			data.put("success", true);
		}else {
			
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
 	}
}
